package com.bjc.tt_shop.service.impl;

import com.bjc.tt_shop.dao.TbItemCatMapper;

import com.bjc.tt_shop.dto.TreeNode;

import com.bjc.tt_shop.pojo.po.TbItemCat;


import com.bjc.tt_shop.pojo.po.TbItemCatExample;
import com.bjc.tt_shop.service.ItemCatService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ItemCatServiceImpl implements ItemCatService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TbItemCatMapper itemCatDao;
    @Override
    public List<TreeNode> listCatByParentId(Long parentId) {
        List<TreeNode> list = new ArrayList<TreeNode>();

        try {
            TbItemCatExample example = new TbItemCatExample();
            TbItemCatExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(parentId);
            List<TbItemCat> itemCatList = itemCatDao.selectByExample(example);
            for (TbItemCat cat : itemCatList) {
                TreeNode tn = new TreeNode();
                tn.setId(cat.getId());
                tn.setText(cat.getName());
                tn.setState(cat.getIsParent() ? "closed" : "open");
                list.add(tn);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
